package com.lujieni.service.impl;

import com.lujieni.dao.PersonDao;
import com.lujieni.domain.po.PersonPO;
import com.lujieni.mybatis.entity.BasicEntity;
import com.lujieni.mybatis.service.AbstractBaseService;
import com.lujieni.service.IPersonService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * @Package: com.lujieni.tkmapper.service.impl
 * @ClassName: PersonServiceImpl
 * @Author: lujieni
 * @Description: Person服务实现类
 * @Date: 2020-12-17 17:36
 * @Version: 1.0
 */
@Service
public class PersonServiceImpl extends AbstractBaseService<PersonPO, PersonDao> implements IPersonService {

    @Override
    public List<PersonPO> queryPersonByName(String name) {
        Example example = new Example(PersonPO.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("myName",name);
        List<PersonPO> list = selectByExample(example);
        return list;
        //List<PersonPO> list = super.dao.queryPersonByName(name);
    }

    @Override
    public List<PersonPO> queryPerson() {
        PersonPO personPO = new PersonPO();
        //personPO.setName("陆哥哥");
        personPO.setMyName("");
        return super.dao.select(personPO);
    }

    @Override
    @Transactional
    public void insertOne() {
        try {
            PersonPO personPO = new PersonPO();
            personPO.setId(11);
            personPO.setMyName("陆捷旎11");
            personPO.setMyAge(30);
            init(personPO);
            dao.insert(personPO);
            int n = 1/0;
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
        }
    }

    private void init(BasicEntity basicEntity){
        basicEntity.setActive(1);
        basicEntity.setCreateTime(new Date());
        basicEntity.setModifyTime(new Date());
        basicEntity.setCreateUserCode("181268");
        basicEntity.setModifyUserCode("181268");
    }
}